package net.kts.subskill.conversation.sqlite;

import java.util.ArrayList;
import java.util.List;

import net.kts.subskill.conversation.model.Conversation;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;

public class ConversationDaoSQLite extends SQLiteOpenHelper {

	public ConversationDaoSQLite(Context context, String name,
			CursorFactory factory, int version) {
		super(context, name, factory, version);
		// TODO Auto-generated constructor stub
	}

	@Override
	public void onCreate(SQLiteDatabase arg0) {
		// TODO Auto-generated method stub

	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		// TODO Auto-generated method stub

	}
	
	
	public List<Conversation> selectConversation(String language) {
		
		if(language.indexOf("-") > 0) {
			language = language.replace("-", "_");
		}
		
		
		String sql = "SELECT rowid, ko, "+language+"  FROM TBL_CONVERSATION";
		
		
		Cursor cursor = getReadableDatabase().rawQuery(sql, null);
		
		cursor.moveToFirst();
		
		List<Conversation> conversationList = null;
		Conversation conversation = null;
		
		int seq = 0 ;
		String ko = null;
		String lang = null;
		
		while(!cursor.isAfterLast()) {
			
			if(conversationList == null) {
				conversationList = new ArrayList<Conversation>();
			}
			
			seq = cursor.getInt(0);
			ko = cursor.getString(1);
			lang = cursor.getString(2);
			
			conversation = new Conversation();
			
			conversation.setSeq(seq);
			conversation.setKo(ko);
			conversation.setLang(lang);
			
			conversationList.add(conversation);
			
			
			cursor.moveToNext();
		}
		
		
		return conversationList;
	}
	
	public void insertConversation(Conversation conversation, String lang) {
		
		String sql = "";
		
		sql += " INSERT INTO TBL_CONVERSATION ";
		sql += " (KO, "+lang+") ";
		sql += " VALUES('"+conversation.getKo()+"', '"+conversation.getLang()+"') ";
		
		getWritableDatabase().execSQL(sql);
		
		
		
	}
	
	public void deleteConversation(Conversation conversation) {
		String sql = "";
		sql += "delete from TBL_CONVERSATION ";
		sql += " where rowid = " +conversation.getSeq();
		
		
		getWritableDatabase().execSQL(sql);
	}

}
